package com.config;/*
 *
 *作者：张继成
 *任务：Swagger2Config接口测试
 *
 *备注：
 *@Date2023/06/04/18:24
 ***/

import com.entity.TokenEntity;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2//开启
@EnableSwaggerBootstrapUI
public class Swagger2Config{

    //配置Swagger的Docket的Bean实例
    @Bean
    public Docket docket(){
        //一些项目设计Token验证，如果项目不存在Token验证这里可以忽略
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        Docket docket=new  Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)//是否启用swagger服务
                .select()
                //RequestHandlerSelectors，配置要扫描的接口方式
                //指定扫描的controller包
                //any()扫描全部、none()不扫描、withClassAnnottation()扫描类上的注解、withMethodAnnotation扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.controller"))
                //过滤路径
//                .paths(PathSelectors.ant(""))
                .build()
                //对应上面的token
               .globalOperationParameters(pars)
                .forCodeGeneration(true);
        docket.securitySchemes(securitySchemes());
        return docket;

    }

    //配置swagger信息apiInfo。覆盖原始的接口重写
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("锦成", "https://gitee.com/jinchening", "577041743@qq.com");
        return new ApiInfo(
                "考研互助系统SwaggerApi接口文档",
                "接口测试",
                "v1.0",
                "https://gitee.com/jinchening",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> securitySchemes = new ArrayList<>();
        securitySchemes.add(new ApiKey("token", "token", "header"));
        System.out.println(securitySchemes);
        return securitySchemes;
    }

}
